package com.cheneyjoo.impl;

import com.alibaba.fastjson.JSON;
import com.cheneyjoo.dao.UserDao;
import com.cheneyjoo.dao.UserExamAnswerDao;
import com.cheneyjoo.model.ResponseMessage;
import com.cheneyjoo.model.UserExamAnswer;
import com.cheneyjoo.services.IUserExamAnswer;

import java.io.IOException;
import java.util.List;

public class UserExamAnswerServiceImpl implements IUserExamAnswer {
    private UserExamAnswerDao userExamAnswerDao;

    public UserExamAnswerServiceImpl() {
        try {
            userExamAnswerDao = new UserExamAnswerDao();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化成绩实现操作类
     *
     * @param userExamAnswerDao
     */
    public UserExamAnswerServiceImpl(UserExamAnswerDao userExamAnswerDao) {
        this.userExamAnswerDao = userExamAnswerDao;
    }

    /**
     * 获取成绩列表
     * @return
     */
    @Override
    public ResponseMessage getAllUserExamAnswers() {
        List<UserExamAnswer> userExamAnswers = userExamAnswerDao.getUserExamAnswers();
        return new ResponseMessage<>(200, "获取成功", userExamAnswers);
    }

    /**
     * 根据用户获取成绩列表
     * @param userID
     * @return
     */
    @Override
    public ResponseMessage getUserExamAnswers(int userID) {
        List<UserExamAnswer> userExamAnswers = userExamAnswerDao.getUserExamAnswers(userID);
        return new ResponseMessage<>(200, "获取成功", userExamAnswers);
    }

    /**
     * 添加成绩
     * @param data
     * @return
     */
    @Override
    public ResponseMessage addUserExamAnswer(String data) {
        UserExamAnswer userExamAnswer= JSON.parseObject(data,UserExamAnswer.class);
        boolean b = userExamAnswerDao.addUserExamAnswer(userExamAnswer);
        return b ? new ResponseMessage<>(200, "添加成功", null) : new ResponseMessage<>(201, "添加失败", null);
    }
}
